// 云函数入口文件
const cloud = require('wx-server-sdk')
const math = require('mathjs')
cloud.init({
    // API 调用都保持和云函数当前所在环境一致
    env: cloud.DYNAMIC_CURRENT_ENV
  })

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database()
    // db.collection("counters")
    //查询体重最大值
    const weightMax = await db.collection('health_rdl')
        .where({
            openId: event.userInfo.openId,
        })
        .orderBy('weight', 'desc')
        .limit(1)
        .get()
        .then(res => {
            console.error("查询体重最大值返回", res)
            if (res.data) {
                return res.data[0]
            }
            return res.data
        })
        .catch(res => {
            console.error("查询体重最大值错误", res)
        })


    //查询体重最小值
    const weightMin = await db.collection('health_rdl')
        .where({
            openId: event.userInfo.openId,
        })
        .orderBy('weight', 'asc')
        .limit(1)
        .get()
        .then(res => {
            console.error("查询体重最小值返回", res)
            if (res.data) {
                return res.data[0]
            }
            return res.data
        })
        .catch(res => {
            console.error("查询体重最小值错误", res)
        })


    //查询日期最大值
    const timeMax = await db.collection('health_rdl')
        .where({
            openId: event.userInfo.openId,
        })
        .orderBy('time', 'desc')
        .limit(1)
        .get()
        .then(res => {
            console.error("查询日期最大值返回", res)
            if (res.data) {
                return res.data[0]
            }
            return res.data
        })
        .catch(res => {
            console.error("查询日期最大值错误", res)
        })


    //查询日期最小值
    const timeMin = await db.collection('health_rdl')
        .where({
            openId: event.userInfo.openId,
        })
        .orderBy('time', 'asc')
        .limit(1)
        .get()
        .then(res => {
            console.error("查询日期最小值返回", res)
            if (res.data) {
                return res.data[0]
            }
            return res.data
        })
        .catch(res => {
            console.error("查询日期最小值错误", res)
        })

    //获取打卡间隔
    let expDays = null
    if (timeMax.time && timeMin.time) {
        let start = new Date(timeMin.time).getTime()
        let end = new Date(timeMax.time).getTime()
        expDays = Math.abs((start - end)) / (1000 * 60 * 60 * 24) + 1;
    }

    //获取实际打卡天数
    const signDays = await db.collection('health_rdl')
        .where({
            openId: event.userInfo.openId,
        }).count().then(res => {
            return res.total
        })


    //变化趋势
    let change = null
    if (timeMax.time && timeMin.time) {
        change = math.number(math.subtract(math.bignumber(timeMax.weight), math.bignumber(timeMin.weight)));
    }

    return {
        weightMax: weightMax,
        weightMin: weightMin,
        timeMax: timeMax,
        timeMin: timeMin,
        expDays: expDays,
        signDays: signDays,
        change: change,
    }
}
